using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Edge.Common.ComponentModel;
using Edge.Common.Utils;
using Edge.Common.ComponentModel;
using Edge.Common.Utils;

namespace Edge.Common.Web.Controls.Ext
{
    [
    DefaultProperty(""),
    ParseChildren(false),
    ToolboxData("<{0}:TextBox runat=\"server\"/>")
    ]
    public class TextBox : System.Web.UI.WebControls.TextBox, IReadOnlySupport
    {



        public TextBox()
        {
        }

        public bool IsLabel
        {
            get
            {
                object state = ViewState["IsLabel"];
                return state != null ? (bool)state : false;
            }
            set
            {
                ViewState["IsLabel"] = value;
            }
        }


        ///<summary>
        ///Gets or sets the behavior mode (single-line, multiline, or password) of the <see cref="T:System.Web.UI.WebControls.TextBox"></see> control.
        ///</summary>
        ///
        ///<returns>
        ///One of the <see cref="T:System.Web.UI.WebControls.TextBoxMode"></see> enumeration values. The default value is SingleLine.
        ///</returns>
        ///
        ///<exception cref="T:System.ArgumentException">The specified mode is not one of the <see cref="T:System.Web.UI.WebControls.TextBoxMode"></see> enumeration values. </exception>
        [Themeable(true)]
        public override TextBoxMode TextMode
        {
            get { return base.TextMode; }
            set { base.TextMode = value; }
        }


        ///<summary>
        ///Gets or sets the number of rows displayed in a multiline text box.
        ///</summary>
        ///
        ///<returns>
        ///The number of rows in a multiline text box. The default is 0, which displays a two-line text box.
        ///</returns>
        ///
        [Themeable(true)]
        public override int Rows
        {
            get { return base.Rows; }
            set { base.Rows = value; }
        }


        [Themeable(true)]
        public override Unit Width
        {
            get { return base.Width; }
            set { base.Width = value; }
        }

        ///<summary>
        ///Renders the <see cref="T:System.Web.UI.WebControls.TextBox"></see> control to the specified <see cref="T:System.Web.UI.HtmlTextWriter"></see> object.
        ///</summary>
        ///
        ///<param name="writer">The <see cref="T:System.Web.UI.HtmlTextWriter"></see> that receives the rendered output.</param>
        protected override void Render(HtmlTextWriter writer)
        {
            if (IsLabel)
            {
                Label lb = new Label();
                lb.ID = this.ClientID;
                if (this.TextMode == TextBoxMode.MultiLine)
                    lb.Text = StringUtils.TextileToHtml(HttpUtility.HtmlEncode(this.Text));
                else
                    lb.Text = HttpUtility.HtmlEncode(this.Text);
                lb.ToolTip = this.ToolTip;
                lb.Width = Width;
                if (StringUtils.IsBlankOrNull(lb.Text))
                    lb.Text = "&nbsp;";
                lb.RenderControl(writer);
            }
            else
            {
                base.Render(writer);
            }
        }

        #region IReadOnlySupport Members

        bool IReadOnlySupport.ReadOnly
        {
            get { return IsLabel; }
            set { IsLabel = value; }
        }

        #endregion
    }
}
